package es.upm.dit.gsi.eclipse.jadex.navigator;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuCreator;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.swt.events.HelpListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.part.MultiPageEditorPart;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.FindReplaceDocumentAdapter;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.editors.text.TextEditor;

//import org.eclipse.wst.xml.ui.internal.tabletree.XMLMultiPageEditorPart;

public class DoubleClickAction implements IAction, ISelectionChangedListener {
	private boolean enabled;
	private boolean checked;
	private ISelection selection;
	private String description;
	private String ID;
	private String toolTipText;
	private String text;
	private IMenuCreator menuCreator;
	private ImageDescriptor imageDescriptor;
	private ImageDescriptor hoverImageDescriptor;
	private ImageDescriptor disabledImageDescriptor;
	private HelpListener helpListener;

	public DoubleClickAction() {
		super();
	}

	@Override
	public void selectionChanged(SelectionChangedEvent event) {
		selection = event.getSelection();
	}

	@Override
	public void addPropertyChangeListener(IPropertyChangeListener listener) {
	}

	@Override
	public int getAccelerator() {
		return 0;
	}

	@Override
	public String getActionDefinitionId() {
		return ID;
	}

	@Override
	public String getDescription() {
		return description;
	}

	@Override
	public ImageDescriptor getDisabledImageDescriptor() {
		return disabledImageDescriptor;
	}

	@Override
	public HelpListener getHelpListener() {
		return helpListener;
	}

	@Override
	public ImageDescriptor getHoverImageDescriptor() {
		return hoverImageDescriptor;
	}

	@Override
	public String getId() {
		return ID;
	}

	@Override
	public ImageDescriptor getImageDescriptor() {
		return imageDescriptor;
	}

	@Override
	public IMenuCreator getMenuCreator() {
		return menuCreator;
	}

	@Override
	public int getStyle() {
		return 0;
	}

	@Override
	public String getText() {
		return text;
	}

	@Override
	public String getToolTipText() {
		return toolTipText;
	}

	@Override
	public boolean isChecked() {
		return checked;
	}

	@Override
	public boolean isEnabled() {
		return this.enabled;
	}

	@Override
	public boolean isHandled() {
		return true;
	}

	@Override
	public void removePropertyChangeListener(IPropertyChangeListener listener) {
	}

	@Override
	public void run() {
		// Here should be the code for double clicking action....
		String marker = "";
		IFile agentFile = null;
		Boolean ok = false;
		if (selection instanceof ITreeSelection) {
			Object elementSelected = ((ITreeSelection) selection)
					.getFirstElement();
			if (elementSelected instanceof Child) {
				Child child = (Child) elementSelected;
				marker = child.getName();
				agentFile = child.getFile();
				ok = true;
			} else if (elementSelected instanceof SubChild) {
				SubChild subchild = (SubChild) elementSelected;
				System.out.println(subchild.getContainer().getName());
				if (subchild.getContainer().getName().equals("plans")) {
					marker = subchild.getHeader();
					agentFile = subchild.getContainer().getFile();
					ok = true;
				} else {
					ok = false;
				}
			}
		}

		final IFile file = agentFile;
		final String markerToFind = marker;

		if (ok) {
			Display.getCurrent().asyncExec(new Runnable() {
				public void run() {
					IWorkbenchPage page = PlatformUI.getWorkbench()
							.getActiveWorkbenchWindow().getActivePage();
					try {
						IEditorPart editor = IDE
								.openEditor(page, file,
										"org.eclipse.wst.xml.ui.internal.tabletree.XMLMultiPageEditorPart");
						if (editor instanceof MultiPageEditorPart) {
							Object editorPage = ((MultiPageEditorPart) editor)
									.getSelectedPage();
							if (!(editorPage instanceof ITextEditor)) {

								IEditorPart[] editorPages = ((MultiPageEditorPart) editor)
										.findEditors(new TextEditor()
												.getEditorInput());
								System.out.println(editorPages.length);
								((MultiPageEditorPart) editor)
										.setActiveEditor(new TextEditor());
							}

							if (editorPage instanceof ITextEditor) {
								ITextEditor editorTextual = (ITextEditor) editorPage;
								IDocument document = editorTextual
										.getDocumentProvider().getDocument(
												editorTextual.getEditorInput());
								FindReplaceDocumentAdapter finder = new FindReplaceDocumentAdapter(
										document);
								try {
									IRegion region = finder.find(0,
											markerToFind, true, true, true,
											false);
									editorTextual.selectAndReveal(
											region.getOffset(),
											region.getLength());
								} catch (BadLocationException e) {
									e.printStackTrace();
								}
							}

						}
					} catch (PartInitException e) {

					}
				}
			});

		}
	}

	@Override
	public void runWithEvent(Event event) {
		run();
	}

	@Override
	public void setActionDefinitionId(String id) {
	}

	@Override
	public void setChecked(boolean checked) {
		this.checked = checked;
	}

	@Override
	public void setDescription(String text) {
		this.description = text;
	}

	@Override
	public void setDisabledImageDescriptor(ImageDescriptor newImage) {
		this.disabledImageDescriptor = newImage;
	}

	@Override
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	@Override
	public void setHelpListener(HelpListener listener) {
		this.helpListener = listener;
	}

	@Override
	public void setHoverImageDescriptor(ImageDescriptor newImage) {
		this.hoverImageDescriptor = newImage;

	}

	@Override
	public void setId(String id) {
		this.ID = id;
	}

	@Override
	public void setImageDescriptor(ImageDescriptor newImage) {
		this.imageDescriptor = newImage;
	}

	@Override
	public void setMenuCreator(IMenuCreator creator) {
		menuCreator = creator;
	}

	@Override
	public void setText(String text) {
		this.text = text;
	}

	@Override
	public void setToolTipText(String text) {
		this.toolTipText = text;
	}

	@Override
	public void setAccelerator(int keycode) {
	}

}
